﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class ProductApiController : ApiController
    {       

        public ProductRepository Get(int? pageNumber, string orderBy, string orderDirection, string productCode, string categoryID, string partnerID,string ownerID, string lotID, string status, string expiredfrom, string expiredto)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(productCode))
                    whereClause = String.Format(whereClause + " AND productCode.Contains(\"{0}\")", productCode);
                if (!String.IsNullOrWhiteSpace(lotID))
                    whereClause = String.Format(whereClause + " AND LotID = {0}", lotID);
                if (!String.IsNullOrWhiteSpace(categoryID))
                    whereClause = String.Format(whereClause + " AND CategoryID = {0}", categoryID);
                if (!String.IsNullOrWhiteSpace(partnerID))
                    whereClause = String.Format(whereClause + " AND PartnerID = {0}", partnerID);
                if (!String.IsNullOrWhiteSpace(ownerID))
                    whereClause = String.Format(whereClause + " AND OwnerID = {0}", ownerID);
                if (!String.IsNullOrWhiteSpace(status))
                    whereClause = String.Format(whereClause + " AND status.Contains(\"{0}\")", status);
                if (expiredfrom.Contains("NaN") || expiredto.Contains("NaN")) return new ProductRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
                return new ProductRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1, ConvertUtility.DateTimeParse(expiredfrom), ConvertUtility.DateTimeParse(expiredto));
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public ProductRepository Get(int? pageNumber, string orderBy, string orderDirection, string productCode)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                return new ProductRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1, ConvertUtility.DateTimeParse(DateTime.Now.AddMonths(3)));
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public int Get(int ownerID, string categoryID, string size)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                whereClause = String.Format(whereClause + " AND ownerID = {0}", ownerID);
                whereClause = String.Format(whereClause + " AND partnerID = {0}", 1);
                if (!String.IsNullOrWhiteSpace(categoryID))
                    whereClause = String.Format(whereClause + " AND categoryID = {0}", categoryID);
                if (!String.IsNullOrWhiteSpace(size))
                    whereClause = String.Format(whereClause + " AND size.Contains(\"{0}\")", size);
                return new ProductRepository().GetCount(whereClause);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public List<ProductComplex> Get(string ownerID, string partnerID, string status)
        {
            string whereClause = "IsDeleted != true";
            if (!String.IsNullOrWhiteSpace(ownerID) && ConvertUtility.IntParse(ownerID) > 0)
                whereClause = String.Format(whereClause + " AND ownerID = {0}", ownerID);
            if (!String.IsNullOrWhiteSpace(partnerID) && ConvertUtility.IntParse(partnerID) > 0)
                whereClause = String.Format(whereClause + " AND partnerID = {0}", partnerID);
            if (!String.IsNullOrWhiteSpace(status))
                whereClause = String.Format(whereClause + " AND status.Contains(\"{0}\")", status);
            return new ProductRepository().GetListProduct(whereClause);
        }

        public Product Get(int? id)
        {
            try
            {
                var item = new Product();
                var repository = new ProductRepository();
                if (id.HasValue && id > 0)
                    item = repository.GetByID(id.Value);
                else
                    item.Barcode = GenerateCode.GetCode(repository.GetNextIndex(), CodeType.ProductTS);
                
                var lotRepository = new LotRepository();
                var partnerRepository = new PartnerRepository();
                var categoryRepository = new CategoryRepository();
                item.Lots = lotRepository.GetListLot();
                item.Partners = partnerRepository.GetListPartner();
                item.Categories = categoryRepository.GetListCategory();
                return item;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var list = new ProductRepository();
                    list.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public ProductRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string productCode, string categoryID, string partnerID, string ownerID, string lotID, string status, string expiredfrom, string expiredto)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var repository = new ProductRepository();
                repository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, productCode, categoryID, partnerID, ownerID, lotID, status, expiredfrom, expiredto);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Product Put(Product item)
        {
            try
            {
                var repository = new ProductRepository();
                int ID = repository.Update(item);

                return Get(ID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public List<ProductComplex> Get()
        {
            try
            {
                return new ProductRepository().GetListProduct();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }
          
    }
}
